package com.hdc.lw.word_server.service;

import com.hdc.lw.word_server.po.User;
import com.hdc.lw.word_server.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;

    public List<User> findAll() {
        return userMapper.findAll();
    }


    public Map getUserByName(User user){
        Map<String,Object> map = new HashMap<>();
        User use = userMapper.getUserByName(user.getUsername());
        if(use==null){
            map.put("code",0);
            map.put("msg","用户不存在！");
            return map;//用户不存在
        }else if(use.getPassword().equals(user.getPassword())){
            map.put("code",1);
            map.put("msg","登录成功！");
            map.put("uid",use.getId());
            return map;//登录成功
        }
        map.put("code",2);
        map.put("msg","密码错误！");
        return map;//密码错误
    }

    public Boolean register(User user){
        int row = userMapper.register(user);
        if(row>0){
            return true;
        }
        return false;
    }

    public Map updatePass(User user,String pass){
        Map<String,String> map = new HashMap<>();
//        System.out.println(user.getUsername());
        User user1 = userMapper.getUserByName(user.getUsername());
//        System.out.println("user1:"+user1);
        if(user1.getPassword().equals(user.getPassword())){
            //密码正确
            user1.setPassword(pass);
            int row = userMapper.updatePass(user1);
            if(row>0){
                map.put("code","200");
                map.put("msg","密码修改成功！");
                map.put("flag","true");
            }else {
                map.put("code","302");
                map.put("msg","密码修改失败！");
                map.put("flag","false");
            }
        }else {
            map.put("code","301");
            map.put("msg","旧密码错误！");
            map.put("flag","false");
        }
        return map;
    }
}
